﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace ConstantCMapEditor3
{
    class BarHolder : Declaration
    {
        TextSprite spriteTxt;

        //讀取資源與命名
        public BarHolder()
        {
            sprite = new Sprite(IntelligentTexture.IntelligentLoad("White"));
            sprite.UserData = this;
            spriteTxt = new TextSprite();
            sprite.SpriteColor = Color.YellowGreen;

            Size = new Vector2(10, 10);
        }

        override public void InstanceInit()
        {
            BarHolder box = new BarHolder();
            box.InstanceReal(this);
        }

        private void InstanceReal(BarHolder Candidate)
        {
            sprite.TouchEvent += MouseEvent;
            spriteTxt.Text = "Selected: ";
            spriteTxt.Text += ClassID = "obj" + DateTime.Now.Second + DateTime.Now.Millisecond + Game1.declarationList.Count;
            spriteTxt.Position = new Vector2(spriteTxt.Width / 2 + 50, spriteTxt.Height / 2 + 200);
            spriteTxt.SpriteColor = Color.Green;
            Game1.lineHolderList.Add(this);
            Game1.declarationList.Add(this);
            Game1.MapEditorScene.AttachChild(sprite);
            sprite.Touchable = true;

            DeclarationDeocrator.SetFourRotationButton(this, spriteTxt, 0, 100);
            DeclarationDeocrator.ChangeRenameButton(spriteTxt, this, 0, 230);
            spriteTxt.Touchable = true;
        }

        private void Delete()
        {
            Game1.lineHolderList.Remove(this);
            Game1.declarationList.Remove(this);
            Game1.MapEditorScene.DettachChild(sprite);
            Game1.Selected = null;
        }

        override public void CandidateInit()
        {
            Position = new Vector2(sprite.Width / 2 + 50, sprite.Height / 2 + 50);

            spriteTxt.Text = ClassID = "BarHolder";
            spriteTxt.Position = new Vector2(spriteTxt.Width / 2 + 0, spriteTxt.Height / 2 + 0);
            spriteTxt.SpriteColor = Color.Green;

            sprite.Touchable = false;
        }

        //產生後被點到
        private void MouseEvent(MouseEvent mouseEvent, Scene sender)
        {
            if (mouseEvent.ClickDownEdgeL) Game1.Selected = this;
            if (mouseEvent.PressedL)
            {
                Position += mouseEvent.Position - mouseEvent.PrePosition;
            }

            if (mouseEvent.PressedM)
            {
                Game1.CoordinateScene.Position += mouseEvent.Position - mouseEvent.PrePosition;
            }

            if (mouseEvent.ClickDownEdgeR && Keyboard.GetState().IsKeyDown(Keys.LeftControl) && Game1.Selected != null)
            {
                Vector2 dis = Position - Game1.Selected.Position;
                Vector2 abs = new Vector2(Math.Abs(dis.X), Math.Abs(dis.Y));

                if (abs.X > abs.Y)
                {
                    Vector2 temp = Game1.Selected.Position;
                    temp.Y = Position.Y;

                    float selfWidth = Size.X;

                    if (RotateR > 1.5 && RotateR < 2) selfWidth = Size.Y;
                    if (RotateR > 1.5 + 3.14 && RotateR < 2 + 3.14) selfWidth = Size.Y;

                    if (temp.X > Position.X)
                        temp.X = Position.X + Game1.AlignMentDistance + selfWidth;
                    else
                        temp.X = Position.X - Game1.AlignMentDistance - selfWidth;

                    Game1.Selected.Position = temp;
                }
                else
                {
                    Vector2 temp = Game1.Selected.Position;
                    temp.X = Position.X;

                    float selfWidth = Size.Y;

                    if (RotateR > 1.5 && RotateR < 2) selfWidth = Size.X;
                    if (RotateR > 1.5 + 3.14 && RotateR < 2 + 3.14) selfWidth = Size.X;


                    if (temp.Y > Position.Y)
                        temp.Y = Position.Y + Game1.AlignMentDistance + selfWidth;
                    else
                        temp.Y = Position.Y - Game1.AlignMentDistance - selfWidth;

                    Game1.Selected.Position = temp;
                }
            }
            

            if (KeyBoardManager.IsKeyEdgeDown(Keys.PageUp) && Game1.CoordinateScene.Scale > 0.5f) Game1.CoordinateScene.Scale -= 0.1f;
            if (KeyBoardManager.IsKeyEdgeDown(Keys.PageDown)) Game1.CoordinateScene.Scale += 0.1f;
            if (KeyBoardManager.IsKeyEdgeDown(Keys.Home)) Game1.CoordinateScene.Scale = 1.0f;
        }

        override public bool ReadBack(String str)
        {
            if (str.Contains("CreateBarHolder"))
            {
                InstanceInit();
                BarHolder box = (BarHolder)Game1.declarationList[Game1.declarationList.Count - 1];
                //Get Property

                String[] temp;
                temp = str.Split(", )".ToCharArray());
                box.ClassID = temp[1];

                int X = Convert.ToInt32(temp[5]);
                int Y = Convert.ToInt32(temp[7]);
                int W = Convert.ToInt32(temp[9]);
                int H = Convert.ToInt32(temp[11]);
                double R = Convert.ToDouble(temp[13].Replace('f', ' '));

                box.Size = new Vector2(W, H);
                box.Position = new Vector2(X, Y);
                box.RotateR = (float)R;


                return true;
            };
            return false;
        }

        //PhysicalFactory.CreateMidBox(this, 393, 410, 0f);
        override public String SaveOut()
        {
            return "BarHolder " + ClassID + " = PhysicalFactory.CreateBarHolder(this, " + (int)Position.X + ", " + (int)Position.Y + ", " + (int)Size.X + ", " + (int)Size.Y + ", " + RotateR + "f,\""+ClassID+"\");";
        }

        override public void SelectedAction()
        {
            Game1.MapEditorUIScene.AttachChild(spriteTxt);
        }

        override public void SelectedUpdate()
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Right)) Position += new Vector2(1, 0);
            if (Keyboard.GetState().IsKeyDown(Keys.Left)) Position -= new Vector2(1, 0);
            if (Keyboard.GetState().IsKeyDown(Keys.Up)) Position -= new Vector2(0, 1);
            if (Keyboard.GetState().IsKeyDown(Keys.Down)) Position += new Vector2(0, 1);
            if (Keyboard.GetState().IsKeyDown(Keys.Q)) RotateR -= 0.01f;
            if (Keyboard.GetState().IsKeyDown(Keys.E)) RotateR += 0.01f;
            if (KeyBoardManager.IsKeyEdgeDown(Keys.Delete)) Delete();

            if (!Keyboard.GetState().IsKeyDown(Keys.LeftControl))
            {
                if (Keyboard.GetState().IsKeyDown(Keys.D)) Size += new Vector2(5, 0);
                if (Keyboard.GetState().IsKeyDown(Keys.S)) Size += new Vector2(0, 5);
                if (Keyboard.GetState().IsKeyDown(Keys.A)) Size -= new Vector2(5, 0);
                if (Keyboard.GetState().IsKeyDown(Keys.W)) Size -= new Vector2(0, 5);
            }
            else
            {
                if (KeyBoardManager.IsKeyEdgeDown(Keys.D)) Size += new Vector2(1, 0);
                if (KeyBoardManager.IsKeyEdgeDown(Keys.S)) Size += new Vector2(0, 1);
                if (KeyBoardManager.IsKeyEdgeDown(Keys.A)) Size -= new Vector2(1, 0);
                if (KeyBoardManager.IsKeyEdgeDown(Keys.W)) Size -= new Vector2(0, 1);
            }

            if (Size.X < 10) Size = new Vector2(10, Size.Y);
            if (Size.Y < 10) Size = new Vector2(Size.X, 10);

            spriteTxt.Text = "Selected: ";
            spriteTxt.Text += ClassID;
            spriteTxt.Text += "\n Position =" + Position.ToString();
            spriteTxt.Text += "\n Size =" + Size.ToString();
            spriteTxt.Text += "\n Rotate =" + RotateR.ToString();

            spriteTxt.Position = new Vector2(spriteTxt.Width / 2, spriteTxt.Height / 2 + 200);
        }

        override public void UnSelectedAction()
        {
            Game1.MapEditorUIScene.DettachChild(spriteTxt);
        }
        //UI

        override public void UISelectedAction()
        {
            Game1.MapEditorUIScene.AttachChild(sprite);
            Game1.MapEditorUIScene.AttachChild(spriteTxt);
        }

        override public void UISelectedUpdate()
        {
        }

        override public void UnUISelectedAction()
        {
            Game1.MapEditorUIScene.DettachChild(sprite);
            Game1.MapEditorUIScene.DettachChild(spriteTxt);
        }

        //UI

        override public Vector2 Position
        {
            get
            {
                return sprite.Position - Size / 2; ;
            }
            set
            {
                sprite.Position = value + Size / 2;
            }
        }

        override public Vector2 Size
        {
            get
            {
                return new Vector2(sprite.Width * sprite.ScaleX, sprite.Height * sprite.ScaleY);
            }
            set
            {
                sprite.ScaleX = value.X / sprite.Width;
                sprite.ScaleY = value.Y / sprite.Height;
            }
        }

        override public float RotateR
        {
            get
            {
                return sprite.Rotate;
            }
            set
            {
                sprite.Rotate = value;
            }
        }
    }
}
